/**
 * Created by leo on 16-12-5.
 */

'use strict';

/**
 * Module dependencies
 */
var mongoose = require('mongoose'),
  Schema = mongoose.Schema;

/**
 * Story Schema
 */
var StorySchema = new Schema({
  _id: {
    type: Schema.ObjectId,
    auto: true
  },
  title: {
    type: String,
    required: true,
    default: getDate
  },
  description: {
    type: String
  },
  creator: {
    type: Schema.ObjectId,
    ref: 'User'
  },
  status: {
    type: String,
    enum: ['open', 'closed'],
    default: 'open'
  },
  start_date: {
    type: Date,
    default: Date.now
  },
  end_date: {
    type: Date,
    default: function() {
      return Date.now() + 30 * 24 * 60 * 60 * 1000; // Default 30 days later
    }
  },
  members: {
    type: [{
      type: Schema.ObjectId,
      ref: 'User'
    }],
    default: []
  },
  photos: {
    type: [{
      type: Schema.ObjectId,
      ref: 'Photo'
    }],
    default: []
  }
}, {
  toObject: {
    getters: true
  }
});

StorySchema.virtual('sid').get(function () { return this._id; });

mongoose.model('Story', StorySchema);

function getDate() {
  return Date.now().toString();
}
